// 设置挂号相关接口
const express = require('express')
const router = express.Router()
const Response = require('../utils/Response.js')

// 引入mysql连接池
const pool = require('../utils/db.js')
const { error } = require('../utils/Response.js')

// 新增挂号
router.post('/register/add',(req,res) =>{
  let {p_hospitalId,p_id,p_name,p_sex,p_IDCard,p_address,p_Department,p_phone,p_doctor,p_time,p_price} = req.body
  let sql = 'insert into patient_Inquiries (p_hospitalId,p_id,p_name,p_sex,p_IDCard,p_address,p_Department,p_phone,p_doctor,p_time,p_price) values (?,?,?,?,?,?,?,?,?,?,?)'
  pool.query(sql,[p_hospitalId,p_id,p_name,p_sex,p_IDCard,p_address,p_Department,p_phone,p_doctor,p_time,p_price],(err,result) =>{
    if(err){
      res.send(Response.error(500,error))
      throw err
    }
    // 将结果返回给客户端
    res.send(Response.ok(result))
  })
})

// 取消挂号
router.delete('/register/delete',(req,res) =>{
  let {p_id} = req.body
  let sql = 'delete from patient_Inquiries where p_id=?'
  pool.query(sql,[p_id],(err,result) =>{
    if(err){
      res.send(Response.error(500,error))
      throw err
    }
    // 将结果返回给客户端
    res.send(Response.ok(result))
  })
})

// 根据医院id查询挂号信息
router.get('/registerByh_id',(req,res) =>{
  let {p_hospitalId} = req.query
  let sql = 'select * from patient_Inquiries where p_hospitalId=?'
  pool.query(sql,[p_hospitalId],(err,result) =>{
    if(err){
      res.send(Response.error(500,error))
      throw err
    }
    // 将结果返回给客户端
    res.send(Response.ok(result))
  })
})

// 根据挂号人姓名查询挂号信息
router.get('/registerByp_id',(req,res) =>{
  let {p_name} = req.query
  let sql = 'select * from patient_Inquiries where p_name=?'
  pool.query(sql,[p_name],(err,result) =>{
    if(err){
      res.send(Response.error(500,error))
      throw err
    }
    // 将结果返回给客户端
    res.send(Response.ok(result))
  })
})

module.exports = router